<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\QrPoster;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class QrPosterController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('海报');
            $content->description('模板');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑');
            $content->description('');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('创建');
            $content->description('');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(QrPoster::class, function (Grid $grid) {
            $grid->model()->orderBy("order",'desc');

            $grid->id('ID')->sortable();
            $grid->column('cover', '模板')->thumb();
            $grid->column('name', '名称');
            $grid->column('type', '类型');
            $grid->column('order', '类型');
            $grid->created_at('创建时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(QrPoster::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name', '模板名称');
            $form->number('order', '排序');
            $form->select('type', '类型')->options(QrPoster::TYPE);
            $form->number('item_id', '数据关联ID')->default(0);
            $form->image('cover', '模板缩略图')->uniqueName();
            $form->image('path', '模板背景')->help("1：NorthWest  |  2：North  |  3：NorthEast|4：West  |     5：Center | 6：East| 7：SouthWest     |     8：South      |     9：SouthEast

            ")->uniqueName();
            $form->ace_editor("params", "配置参数")->default(file_get_contents(storage_path('qr_poster_temp.json')))->help("123");

            $form->saving(function (Form $form) {
                $form->params = json_encode(json_decode($form->params));
            });
        });
    }
}
